<?php
 namespace Admin\Action;;
/**
 * ============================================================================
 * WSTMall开源商城
 * 官网地址:http://www.wstmall.net
 * 联系QQ:707563272
 * ============================================================================
 * 订单结算控制器
 */
class OrderSettlementsAction extends BaseAction{
	/**
	 * 订单结算列表
	 */
	public function index(){
		$this->isLogin();
		$this->checkPrivelege('js_00');
		//获取地区信息
		$m = D('Admin/Areas');
		$this->assign('areaList',$m->queryShowByList(0));
		$rs = array('status'=>1);
		$m = D('Admin/OrderSettlements');
    	$page = $m->queryByPage();
    	$pager = new \Think\Page($page['total'],$page['pageSize'],I());
    	$page['pager'] = $pager->show();
    	$this->assign('Page',$page);
    	$this->assign('areaId1',(int)I('areaId1'));
    	$this->assign('areaId2',(int)I('areaId2'));
    	$this->assign('areaId3',(int)I('areaId3'));
    	$this->assign('shopName',I('shopName'));
    	//$this->assign('isFinish',(int)I('isFinish',-1));
		$this->display("/ordersettlements/list");
	}
	
	/**
	 * 跳去结算页面
	 */
	public function toSettlement(){
		$this->isLogin();
		$this->checkPrivelege('js_04');
		$m = D('Admin/OrderSettlements');
	    if((int)I('id')>0){
			$object = $m->get();
			$this->assign('object',$object);
		}
		$this->display("/ordersettlements/settlement");
	}
	
	/**
	 * 结算
	 */
	public function settlement(){
		$this->isLogin();
		$this->checkPrivelege('js_04');
		$m = D('Admin/OrderSettlements');
		$rs = $m->settlement();
		$this->ajaxReturn($rs);
	}
	
    /**
	 * 跳去结算页面
	 */
	public function toView(){
		$this->isLogin();
		$this->checkPrivelege('js_04');
		$m = D('Admin/OrderSettlements');
	    if((int)I('id')>0){
			$object = $m->getDetail();
			$this->assign('object',$object);
		}
		$this->display("/ordersettlements/view");
	}



    /**
     * 跳到更多结算订单页面
     */

    public function toMore(){
        $this->isLogin();
        $this->checkPrivelege('js_00');
        $m = D('Admin/OrderSettlements');
        if((int)(I("get.shopId")>0)){
            $page = $m->getMore();
            $pager = new \Think\Page($page['total'],$page['pageSize'],I());
            $page['pager'] = $pager->show();
            $this->assign('Page',$page);
        }

        $this->assign('isFinish',(int)I('isFinish',-1));
        $this->assign("settlementNo",I("settlementNo"));
        $this->display("ordersettlements/list_more");
    }


    /**
     * 导出Ecxel
     */
    public function exportExcel(){
        Vendor("PHPExcel.PHPExcel");
        Vendor("PHPExcel.PHPExcel.Writer.Excel2007");
        $m = D("OrderSettlements");
        $data = $m->getExportData();

        $phpExcel = new \PHPExcel();
        $phpExcel->setActiveSheetIndex(0);//设置活动工作表
        $activeSheet = $phpExcel->getActiveSheet();

        $activeSheet->setTitle($data["shopName"]);
        $activeSheet->setCellValue("A1","商家编号")->setCellValue("B1",$data["shopId"])->setCellValue("C1","商家：")
            ->setCellValue("D1",$data["shopName"])->setCellValue("E1","可提现：")->setCellValue("F1",$data["settlemtMonney"])
            ->setCellValue("G1","冻结：")->setCellValue("H1",$data["unSettlementMonney"]);
        $activeSheet->setCellValue("A2","序号")->setCellValue("B2","结算编号")->setCellValue("C2","订单金额")->setCellValue("D2","结算金额")
            ->setCellValue("E2","平台服务费")->setCellValue("F2","结算时间")->setCellValue("G2","备注")->setCellValue("H2","状态");

        foreach($data["orderList"] as $key=>$order){
            $k = $key+3;
            $activeSheet->setCellValue("A$k",$key)->setCellValue("B$k",$order["settlementNo"])->setCellValue("C$k",$order["orderMoney"])->setCellValue("D$k",$order['settlementMoney'])
                ->setCellValue("E$k","")->setCellValue("F$k",$order['finishTime'])->setCellValue("G$k",$order['remarks'])->setCellValue("H$k",$order["isFinish"]?"已结算":"未结算");

        }

        $activeSheet->getColumnDimension("B")->setAutoSize(true);
       // $activeSheet->getStyle()->getAlignment()->setHorizontal(true);

        $objectWriter = new \PHPExcel_Writer_Excel2007($phpExcel);
        header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
        header('Content-Disposition: attachment;filename="'.$data["shopName"].'.xlsx"');
        header('Cache-Control: max-age=0');
        $objectWriter->save('php://output');
    }


    /**
     * 会员卡结算列表
     */
    public function memberCardList(){
        $model = M('MembershipCardLog');
        $where = array();
        $count = $model->count('id');
        $page = new \Think\Page($count,C('PAGE_SIZE'));
        $list = $model->alias('mcl')->join('left join __ORDERS__ o on mcl.order_id = orderId')->
        join("left join __SHOPS__ s on o.shopId = CONCAT(',',s.shopId,',')")->join('__MEMBERSHIP_CARD__ mc on mcl.membership_card_id = mc.id')
        ->where($where)->field('mcl.id,mcl.op_time,mc.name,mcl.money,s.shopName')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('page',$page->show());
        $this->assign('list',$list);

        $this->display('ordersettlements/member_card_list');

    }
	
};
?>